/*
* @Author: Administrator
* @Date:   2020-06-19 18:13:51
* @Last Modified by:   K.EL
* @Last Modified time: 2020-07-22 16:52:29
*/
module.exports={
    appid : '5678910',
    appsecret : 'mimia87654321mami',
	token : '',
    url :{
        LOGIN       :'/tools/login',
        HOME        :'/tools/index',
    },
	request(url, body , complete, error, tar ){
        var nowTime = new Date().getTime(); //获取当前时间戳
        body.time = Math.ceil(nowTime/1000)+""; //取时间到秒
        body.appid = this.appid;
        body.sign = this.md5Enc(body.time, tar);   //生成md5签名
        var option  = {
            method:'post',
            url:url,
            headers:{
                token: tar.Common.cookie('token'),
            },
            body:body
        };
        var Common = this;
        tar.$http(option).then(function(ret){
            // console.log(ret.body,'请求成功了');
            complete(ret.body);
        },function(ret){
            // console.log(ret.body,'请求报错了');
            error(ret.body);
        });
    },
    //上传图片
    requestUpload(url, file, complete, error, tar ){
        

        var option  = {
            method:'post',
            headers: {
                token: tar.Common.cookie('token'),
                'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryWwE7y8P3JK82rxs'
            }
        };
        //添加图片
        let formdata = new FormData();
        formdata.append('file', file);

        //添加其他参数
        var nowTime = new Date().getTime(); //获取当前时间戳
        var time    = Math.ceil(nowTime/1000); //取时间到秒
        var sign    = this.md5Enc(time, tar);   //生成md5签名
        var appid   = this.appid;
        var token   = tar.Common.cookie('token');
        formdata.append('appid',appid);
        formdata.append('sign',sign);
        formdata.append('time',time);
        formdata.append('token',token);
        tar.$http.post(url,  formdata, option).then(function(ret){
            complete(ret.body);
        },function(ret){
            error(ret.body);
        })
    },
    //跳转
    jump(tar, url , seconds ){
        setTimeout(function(){
            tar.$router.push({path:url});
        },seconds)
    },
    cookie( name ){
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return decodeURIComponent(arr[2]);
        else
            return null;
    },
    setCookie( key ,value){
        var Days = 30; var exp = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = key + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/";
    },
    removeCookie(key){
        var exp = new Date();
        exp.setTime( exp.getTime() - 1000 );
        document.cookie = key + "="+ escape (false) + ";expires=" + exp.toGMTString()+";path=/";
        document.cookie = key + "="+ escape (false) + ";expires=" + exp.toGMTString()+";path=/";
    },

    md5Enc(time, tar){
        var sign = tar.$md5(this.appid+this.appsecret+time+"");
        return sign;
    }
}